var t = getApp(), a = require("../../components/panzoom.js"), r = null, e = null;

Page({
    data: {
        server: t.server,
        serverUrl: t.serverUrl,
        cates: [],
        currCate: 0,
        currPrd: 0,
        currPic: 0,
        showBbar: !0,
        black: !1,
        lastX: 0,
        lastY: 0,
        touchs: 0,
        showTip: !0,
        transform3d: "transform-origin: 0% 0%;transform:scale3d(1,1,1);",
        auto: !1
    },
    onLoad: function(e) {
        var s = this;
        t.request({
            url: t.server + "/productApi/getFullSceneCat",
            method: "POST",
            data: {
                token: t.token,
                page_size: 999,
                cur_page: 1
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var a = t.data.result.data.filter(function(t) {
                    return t.id === e.cateId;
                })[0];
                s.init(a, e);
            }
        }), r = new a(this);
    },
    init: function(t, a) {
        var r = t.children, e = !0, s = !1, i = void 0;
        try {
            for (var c, n = r[Symbol.iterator](); !(e = (c = n.next()).done); e = !0) c.value.prds = [];
        } catch (t) {
            s = !0, i = t;
        } finally {
            try {
                !e && n.return && n.return();
            } finally {
                if (s) throw i;
            }
        }
        this.setData({
            cates: r
        });
        for (var o = 0; o < r.length; o++) this.loadPrds(o, a);
    },
    loadPrds: function(a, r) {
        var e = this.data.cates[a], s = e.id === r.subCateId;
        s && this.setData({
            currCate: a
        });
        var i = this;
        t.request({
            url: t.server + "/productApi/getFullSceneCatInfo",
            method: "POST",
            data: {
                token: t.token,
                page_size: 999,
                cur_page: 1,
                cid: e.id
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function(t) {
                var e = {}, c = t.data.result.list;
                e["cates[" + a + "].prds"] = c;
                var n = !1;
                if (s) for (var o = 0; o < c.length; o++) if (c[o].id === r.id) {
                    e.currPrd = o, n = !0;
                    break;
                }
                i.setData(e), n && i.startTimer();
            }
        });
    },
    cateSwitch: function(t) {
        var a = t.currentTarget.dataset.index;
        this.setData({
            currCate: a,
            currPrd: 0,
            currPic: 0
        }), r.reset(), this.startTimer();
    },
    prdSwitch: function(t) {
        var a = t.currentTarget.dataset.index;
        this.setData({
            currPrd: a,
            currPic: 0
        }), r.reset(), this.startTimer();
    },
    picSwitch: function(t) {
        this.setData({
            currPic: t
        }), r.reset(), this.startTimer();
    },
    bbarToggle: function() {
        this.setData({
            showBbar: !this.data.showBbar
        });
    },
    switchToggle: function(t) {
        this.setData({
            black: !this.data.black
        });
    },
    handletouchmove: function(t) {
        if (2 !== t.touches.length) {
            if (!(this.data.touchs > 1)) {
                var a = t.touches[0].pageX, e = t.touches[0].pageY;
                a - this.data.lastX < 0 ? this.nextPic(1) : a - this.data.lastX > 0 && this.nextPic(-1), 
                this.data.lastX = a, this.data.lastY = e, this.setData({
                    text: ""
                });
            }
        } else r.touchmove(t);
    },
    nextPic: function(t) {
        try {
            var a = this.data.currPic + t;
            a < 0 ? a = this.data.cates[this.data.currCate].prds[this.data.currPrd].pic_product.pic.length - 1 : a > this.data.cates[this.data.currCate].prds[this.data.currPrd].pic_product.pic.length - 1 && (a = 0), 
            this.setData({
                currPic: a
            }), this.data.showTip && this.setData({
                showTip: !1
            });
        } catch (t) {
            console.log(t);
        }
    },
    handletouchend: function(t) {
        2 !== t.touches.length ? (this.setData({
            showTip: !0
        }), this.startTimer()) : r.touchend(t);
    },
    handletouchstart: function(t) {
        if (2 !== t.touches.length) {
            var a = t.touches[0].pageX, e = t.touches[0].pageY;
            this.data.lastX = a, this.data.lastY = e, this.data.touchs = t.touches.length, this.stopTimer();
        } else r.touchstart(t);
    },
    nextTimer: function(t) {
        var a = this;
        if (!0 !== t) {
            var r = a.data.currPic - 1;
            r < 0 ? r = a.data.cates[a.data.currCate].prds[a.data.currPrd].pic_product.pic.length - 1 : r > a.data.cates[a.data.currCate].prds[a.data.currPrd].pic_product.pic.length - 1 && (r = 0), 
            a.setData({
                currPic: r
            });
        }
        e = setTimeout(function() {
            a.nextTimer();
        }, 50);
    },
    startTimer: function() {
        if (this.data.auto) {
            this.stopTimer();
            var t = this;
            t.data.cates && t.data.cates[t.data.currCate] && t.data.cates[t.data.currCate].prds && t.data.cates[t.data.currCate].prds[t.data.currPrd] && this.nextTimer(!0);
        }
    },
    stopTimer: function() {
        e && (clearInterval(e), e = null);
    },
    onUnload: function() {
        this.stopTimer();
    },
    onHide: function() {
        this.stopTimer();
    },
    onShow: function() {
        this.startTimer();
    },
    play: function(t) {
        this.setData({
            auto: !this.data.auto
        }), this.data.auto ? this.startTimer() : this.stopTimer();
    },
    onShareAppMessage: function() {}
});